#include "networkplayer.h"

#include <physics/pworld.h>

#include <QDomElement>
#include <QUdpSocket>

NetworkPlayer::NetworkPlayer(PWorld *world, PVector startPos, double startAngle, QString name, QObject *parent, bool createShip)
    : Player(world, startPos, startAngle, name, parent, createShip)
{
    mySocket = 0;
}

NetworkPlayer::NetworkPlayer(Player *player, PWorld *world) :
        Player(world, player->getStartPos(), player->getStartAngle(), player->getName(), world, false)
{
    setColor(player->getColor());
    mySocket = 0;
}


void NetworkPlayer::serialize(QDomDocument *doc, QDomElement *elem) const {
    elem->setAttribute("type", "LocalPlayer");
    Player::serialize(doc, elem);

    QDomElement keyUp = doc->createElement("KeyUp");
    QDomText keyUp_v = doc->createTextNode(QString("%1").arg(myKeyUp));
    keyUp.appendChild(keyUp_v);
    elem->appendChild(keyUp);

    QDomElement keyDown = doc->createElement("KeyDown");
    QDomText keyDown_v = doc->createTextNode(QString("%1").arg(myKeyDown));
    keyDown.appendChild(keyDown_v);
    elem->appendChild(keyDown);

    QDomElement keyRight = doc->createElement("KeyRight");
    QDomText keyRight_v = doc->createTextNode(QString("%1").arg(myKeyRight));
    keyRight.appendChild(keyRight_v);
    elem->appendChild(keyRight);

    QDomElement keyLeft = doc->createElement("KeyLeft");
    QDomText keyLeft_v = doc->createTextNode(QString("%1").arg(myKeyLeft));
    keyLeft.appendChild(keyLeft_v);
    elem->appendChild(keyLeft);

}

void NetworkPlayer::setKeys(Qt::Key up, Qt::Key down, Qt::Key right, Qt::Key left) {
    myKeyUp = up;
    myKeyDown = down;
    myKeyRight = right;
    myKeyLeft = left;

//    myKeyManager->addEvent(myKeyUp, this, SLOT(pressAccelerate()), SLOT(releaseAccelerate()));
//    myKeyManager->addEvent(myKeyDown, this, SLOT(pressFire()), SLOT(releaseFire()));
//    myKeyManager->addEvent(myKeyLeft, this, SLOT(pressLeft()), SLOT(releaseLeft()));
//    myKeyManager->addEvent(myKeyRight, this, SLOT(pressRight()), SLOT(releaseRight()));

//    myKeyManager->addEvent(myKeyUp, this, SLOT(sendAccelerate()), SLOT(voidf()));
//    myKeyManager->addEvent(myKeyDown, this, SLOT(sendFire()), SLOT(voidf()));
//    myKeyManager->addEvent(myKeyLeft, this, SLOT(sendLeft()), SLOT(voidf()));
//    myKeyManager->addEvent(myKeyRight, this, SLOT(sendRight()), SLOT(voidf()));
}



NetworkPlayer *NetworkPlayer::deserialize(QDomNode e, PWorld *world) {
    Player *player = Player::deserialize(e, 0);
    NetworkPlayer *networkPlayer = new NetworkPlayer(player, world);
    delete player;

    Qt::Key keyUp = Qt::Key_No;
    Qt::Key keyDown = Qt::Key_No;
    Qt::Key keyLeft = Qt::Key_No;
    Qt::Key keyRight = Qt::Key_No;
    QDomNode n = e.firstChild();
    while (!n.isNull()) {
        QString tagname = n.toElement().tagName();
        if (tagname == "KeyUp") {
            keyUp = (Qt::Key)n.firstChild().toText().data().toInt();
        } else if (tagname == "KeyDown") {
            keyDown = (Qt::Key)n.firstChild().toText().data().toInt();
        } else if (tagname == "KeyLeft") {
            keyLeft = (Qt::Key)n.firstChild().toText().data().toInt();
        } else if (tagname == "KeyRight") {
            keyRight = (Qt::Key)n.firstChild().toText().data().toInt();
        }
        n = n.nextSibling();
    }
    networkPlayer->setKeys(keyUp, keyDown, keyRight, keyLeft);

    return networkPlayer;
}

QString NetworkPlayer::getType() const {
    return QString("NetworkPlayer");
}

void NetworkPlayer::setSocket(QUdpSocket *socket) {
    mySocket = socket;
}

void NetworkPlayer::readPendingDatagramms() {

}
